package com.glela.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestGuest;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.SecurityService;

/**
 * @author liujie
 * @date 2016年6月15日
 *
 */
@Controller
@RequestMapping(value = "/security")
public class SecurityController {

	private static Logger logger = LoggerFactory
			.getLogger(SecurityController.class);

	@Autowired
	private SecurityService securityService;

	/**
	 * 获取匿名用户
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/generateGuest", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> generateGuest(@RequestBody RequestGuest param) {
		try {
			String result = securityService.getDeviceLink(param);
			if ("".equals(result)) {
				return new ResponseEntity<>(new ResponseBaseModel(
						ErrorEnum.C20114), HttpStatus.OK);
			}

			// User guest = userService.getUserByName(result);
			// if(guest == null) {
			// RequestUserModel userModel = new RequestUserModel();
			// userModel.setUserName(result);
			// userModel.setPassword("null");
			// userModel.setMobilePhone("null");
			// userModel.setType(2);
			// userService.saveRegister(userModel);
			// }
			return new ResponseEntity<>(new ResponseResultModel<>(result),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("匿名用户生成异常:", e);
			return new ResponseEntity<>(
					new ResponseBaseModel(ErrorEnum.C20114), HttpStatus.OK);
		}
	}
}
